public class PasswordStateMachine {

    // 状态枚举
    enum State {
        READING,        // 读取键入字符
        VERIFYING,      // 验证密码
        LOCKED,         // 锁定控制面板
        SYSTEM_MENU     // 系统功能选择
    }

    private State currentState = State.READING;

    private final String correctPassword = "1234"; // 正确密码
    private StringBuilder inputBuffer = new StringBuilder();
    private int failCount = 0;

    private long lockStartTime = 0; // 锁定开始时间
    private final long LOCK_DURATION_MS = 120 * 1000; // 120 秒

    // ----------------------------------------------------
    // 切换状态
    // ----------------------------------------------------
    private void transitionTo(State newState) {
        System.out.println("【状态切换】" + currentState + " -> " + newState);
        currentState = newState;
    }

    // ----------------------------------------------------
    // 主入口（输入一个字符）
    // ----------------------------------------------------
    public void inputChar(char c) {

        switch (currentState) {

            case READING:
                handleReading(c);
                break;

            case VERIFYING:
                handleVerify();
                break;

            case LOCKED:
                handleLocked();
                break;

            case SYSTEM_MENU:
                System.out.println("已进入系统功能选择界面。操作结束。");
                break;
        }
    }

    // ----------------------------------------------------
    // 状态：读取键入字符
    // ----------------------------------------------------
    private void handleReading(char c) {

        // 输入字符
        inputBuffer.append(c);
        System.out.println("当前输入：" + inputBuffer);

        // 输入字符长度不足 4 → 继续输入
        if (inputBuffer.length() < 4) {
            System.out.println("输入字符长度小于 4，继续输入...");
            return;
        }

        // 输入字符长度 == 4 → 进入验证
        if (inputBuffer.length() == 4) {
            transitionTo(State.VERIFYING);
            handleVerify();
        }
    }

    // ----------------------------------------------------
    // 状态：验证密码
    // ----------------------------------------------------
    private void handleVerify() {

        String inputPassword = inputBuffer.toString();

        System.out.println("开始验证密码: " + inputPassword);

        if (inputPassword.equals(correctPassword)) {
            System.out.println("密码正确！");
            transitionTo(State.SYSTEM_MENU);
            return;
        }

        // 密码错误
        failCount++;
        System.out.println("密码错误，第 " + failCount + " 次");

        // 清空输入
        inputBuffer = new StringBuilder();

        // 错误次数达到 3 → 锁定
        if (failCount >= 3) {
            System.out.println("连续 3 次密码错误，进入锁定状态！");
            lockStartTime = System.currentTimeMillis();
            transitionTo(State.LOCKED);
            return;
        }

        // 错误次数不足 3 → 回到输入状态
        transitionTo(State.READING);
    }

    // ----------------------------------------------------
    // 状态：锁定面板 120 秒
    // ----------------------------------------------------
    private void handleLocked() {

        long now = System.currentTimeMillis();

        // 判断是否超过锁定时间
        if (now - lockStartTime >= LOCK_DURATION_MS) {
            System.out.println("锁定时间已过 120 秒，解锁成功");
            failCount = 0;
            inputBuffer = new StringBuilder();
            transitionTo(State.READING);
        } else {
            long remain = (LOCK_DURATION_MS - (now - lockStartTime)) / 1000;
            System.out.println("仍在锁定中（" + remain + " 秒）...");
        }
    }
}
